package com.seisys.service.system.menu;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.seisys.dao.DaoSupport;
import com.seisys.entity.system.Menu;
import com.seisys.util.PageData;

@Service("menuService")
public class MenuService {

	@Resource(name = "daoSupport")
	private DaoSupport dao;

	public void deleteMenuById(String MENU_ID) throws Exception {
		dao.save("MenuMapper.deleteMenuById", MENU_ID);

	}

	public PageData getMenuById(PageData pd) throws Exception {
		return (PageData) dao.findForObject("MenuMapper.getMenuById", pd);

	}

	// 取最大id
	public PageData findMaxId(PageData pd) throws Exception {
		return (PageData) dao.findForObject("MenuMapper.findMaxId", pd);

	}

	public List<Menu> listAllParentMenu() throws Exception {
		return (List<Menu>) dao.findForList("MenuMapper.listAllParentMenu",
				null);

	}

	public void saveMenu(Menu menu) throws Exception {
		if (menu.getMENU_ID() != null && menu.getMENU_ID() != "") {
			// dao.update("MenuMapper.updateMenu", menu);
			dao.save("MenuMapper.insertMenu", menu);
		} else {
			dao.save("MenuMapper.insertMenu", menu);
		}
	}

	public List<Menu> listSubMenuByParentId(String parentId) throws Exception {
		return (List<Menu>) dao.findForList("MenuMapper.listSubMenuByParentId",
				parentId);

	}

	public List<Menu> getTriMenuList(String parentId) throws Exception {
		return (List<Menu>) dao.findForList("MenuMapper.getTriMenuList",
				parentId);
	}

	public List<Menu> listAllMenu() throws Exception {
		List<Menu> rl = this.listAllParentMenu();
		for (Menu menu : rl) {
			List<Menu> subList = this.listSubMenuByParentId(menu.getMENU_ID());
			if (subList.size() != 0) {
				for (Menu triMenu : subList) {
					List<Menu> triMenuList = new ArrayList<Menu>();
					if (null != triMenu.getMENU_TYPE()
							&& triMenu.getMENU_TYPE().equals("Y")) {
						triMenuList = this.getTriMenuList(triMenu.getMENU_ID());
					}
					triMenu.setSubMenu(triMenuList);
				}
			}
			menu.setSubMenu(subList);
		}
		return rl;
	}
	
	public List<Menu> listAllMenuForSelect() throws Exception {
		return (List<Menu>) dao.findForList("MenuMapper.listAllMenuForSelect",
				null);

	}

	public List<Menu> listAllSubMenu() throws Exception {
		return (List<Menu>) dao.findForList("MenuMapper.listAllSubMenu", null);

	}

	/**
	 * 编辑
	 */
	public PageData edit(PageData pd) throws Exception {
		return (PageData) dao.findForObject("MenuMapper.updateMenu", pd);
	}

	/**
	 * 保存菜单图标 (顶部菜单)
	 */
	public PageData editicon(PageData pd) throws Exception {
		return (PageData) dao.findForObject("MenuMapper.editicon", pd);
	}

	/**
	 * 获取所有MenuId
	 * 
	 * @return
	 * @throws Exception
	 */
	public List<String> getAllMenuId() throws Exception {
		return (List<String>) dao.findForList("MenuMapper.getAllMenuId", null);
	}

	public Map<String, Object> getMenuUrl() throws Exception {
		return (Map<String, Object>) dao.findForObject("MenuMapper.getMenuUrl", null);
	}
}
